package dblayer;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import modellayer.Company;
import modellayer.JobPosition;
import modellayer.KeyPerson;
import modellayer.Service;
import modellayer.ZipCode;

/**
 * The ServiceDB class to exchange data betwixt SQL database and java
 * @author Allan Eriksen, Andreas Rye, Casper Madsen, Christian Stidsborg, Stefan Bols
 */

public class ServiceDB
{
	private Connection con;
	/**
	 * @param con
	 */
	
	/**
	 * Constructor for the ServiceDB class, also creates a connection to the SQL Database
	 */
	public ServiceDB()
	{
		con = DBConnection.getInstance().getDBcon();
	}
	
	/**
	 * Search method using serviceId
	 * @param serviceId
	 * @return singleWhere(wClause)
	 */
	public Service findServiceById(int serviceId)
	{
		String wClause = " serviceId='"+serviceId+"' ";
		
		return singleWhere(wClause);
	}
	
	/**
	 * singleWhere method to execute a method in the database
	 * @param wClause
	 * @return serviceObj
	 */
	private Service singleWhere(String wClause)
	{
		ResultSet results;
		Service serviceObj = null;
		
		String query = buildQuery(wClause);
		
		try
		{
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			results = stmt.executeQuery(query);
			
			if (results.next())
			{
				serviceObj = buildService(results);
			}
			else
			{
				serviceObj = null;
			}
		}
		catch (Exception e)
		{
			System.out.println("SQL query: " +query);
			System.out.println("Error: " +e);
		}
		
		return serviceObj;
	}
	
	/**
	 * A method which uses wClause parameters to construct a query, in a format that the SQL database understands
	 * @param wClause
	 * @return query
	 */
	private String buildQuery(String wClause)
	{
		String query = "SELECT serviceId, name FROM sem2_Service";
		
		if (!wClause.isEmpty())
		{
			query += " WHERE " +wClause;
		}
		
		return query;
	}
	
	/**
	 * A method which extracts data from the database into Java
	 * @param results
	 * @return serviceObj
	 */
	private Service buildService(ResultSet results)
	{
		Service serviceObj = null;
		
		try
		{
			serviceObj = new Service(results.getInt("serviceId"), results.getString("name"));
		}
		catch (SQLException e)
		{
			System.out.println("Error building Service object! \n Error: " +e);
		}
		
		return serviceObj;
	}
	
	public ArrayList<Service> listAllServices()
	{
		String query = "SELECT sem2_Service.* FROM sem2_Service";
		
		return miscWhere(query);
	}
	
	private ArrayList<Service> miscWhere(String query)
	{
		ArrayList<Service> sList = new ArrayList<Service>();
		ResultSet results;
		
		try
		{
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			results = stmt.executeQuery(query);
			
			while (results.next())
			{
				sList.add(buildService(results));
			}
		}
		catch (Exception e)
		{
			System.out.println("SQL query ServiceDB: "+query);
			System.out.println("	error: "+e);
		}
		
		return sList;
	}
	
}
